package client;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;

import server.IServer;
import server.RMISSLClientSocketFactory;
import server.RMISSLServerSocketFactory;
/**
 * Classe Client that contains information about our client
 * @author Thibault MERLE & Moritz FISCHER *
 */
public class Client extends UnicastRemoteObject implements IClient, Serializable{

	private static final long serialVersionUID = 1L;
	private HashMap<String, IClient> listFriends;
	private IServer server;
	private String username;

	
	public Client(String serverLocation) throws RemoteException {
		super(0, new RMISSLClientSocketFactory(), new RMISSLServerSocketFactory());
		listFriends = new HashMap<String, IClient>();
		username = "";
		try {
			this.server = (IServer) Naming.lookup("rmi://" + serverLocation + "/Server");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}  
			
	}
	
	@Override
	public synchronized void notifyLeave(String user) throws RemoteException {
		System.out.println(user + " left us.");
		listFriends.remove(user);
		System.out.println(ConsoleClient.PROMPT);
	}

	@Override
	public synchronized void notifyEnter(String user) throws RemoteException {
		System.out.println("Arrival of " + user);
		System.out.print(ConsoleClient.PROMPT);
	}

	@Override
	public synchronized void sendPrivateMessage(String sender, String msg)
			throws RemoteException {
		System.out.println(sender + " said:" + msg);
		System.out.print(ConsoleClient.PROMPT);
	}
	
	public void addKnownIClient(String user, IClient iclient) {
		this.listFriends.put(user, iclient);
	}
	
	public IServer getServer() {
		return server;
	}
	
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public HashMap<String, IClient> getListFriends() {
		return listFriends;
	}
}
